package com.chenfengyao.gamereversi.widget

import android.animation.ValueAnimator
import android.content.Context
import android.graphics.Canvas
import android.graphics.Color
import android.graphics.Paint
import android.util.AttributeSet
import android.util.Log
import android.view.View
import androidx.core.content.res.ResourcesCompat
import com.chenfengyao.gamereversi.R
import com.chenfengyao.gamereversi.core.ChessPiece
import kotlin.math.abs
import kotlin.math.min
import kotlin.random.Random

class ReversiCell @JvmOverloads constructor(
    context: Context, attrs: AttributeSet? = null, defStyleAttr: Int = 0
) : View(context, attrs, defStyleAttr) {


    private val blackColor: Int by lazy {
        ResourcesCompat.getColor(resources, R.color.chess_piece_black, null)
    }

    private val whiteColor: Int by lazy {
        ResourcesCompat.getColor(resources, R.color.chess_piece_white, null)
    }

    var chessPiece: ChessPiece = ChessPiece.NONE
        private set

    private val paint = Paint().apply {
        style = Paint.Style.FILL
        isAntiAlias = true
    }

    private val chessPieceSize: Float by lazy {
        min(width, height) * 0.75f
    }

    private var animProgress = 0f

    var index = 0

    override fun onDraw(canvas: Canvas) {
        super.onDraw(canvas)
        if (chessPiece == ChessPiece.BLACK) {
            paint.color = blackColor
            canvas.drawCircle(width / 2f, height / 2f, chessPieceSize / 2 * animProgress, paint)
        } else if (chessPiece == ChessPiece.WHITE) {
            paint.color = whiteColor
            canvas.drawCircle(width / 2f, height / 2f, chessPieceSize / 2 * animProgress, paint)
        }
    }

    /**
     * 更新棋子
     */
    fun updateChessPiece(chessPiece: ChessPiece) {
        if (this.chessPiece == chessPiece) {
            return
        }

        if (chessPiece == ChessPiece.NONE) {
            this.chessPiece = ChessPiece.NONE
            invalidate()
        }

        if (this.chessPiece == ChessPiece.NONE) {
            startCreateAnim(chessPiece)
        } else {
            startChangeAnim(chessPiece)
        }
    }

    /**
     * 开始改变动画
     */
    private fun startChangeAnim(target: ChessPiece) {
        Log.d("ReversiCell", "startChangeAnim: $target")
        val anim = ValueAnimator.ofFloat(1f, 0f, -1F)
        anim.duration = 600
        anim.interpolator = android.view.animation.OvershootInterpolator()
        anim.addUpdateListener {
            val value = it.animatedValue as Float
            if (value < 0) {
                chessPiece = target
            }
            animProgress = abs(it.animatedValue as Float)
            invalidate()
        }
        anim.start()
    }

    /**
     * 开始创建动画
     */
    private fun startCreateAnim(target: ChessPiece) {
        Log.d("ReversiCell", "startCreateAnim: $target")
        this.chessPiece = target
        val anim = ValueAnimator.ofFloat(0f, 1f)
        anim.duration = 300
        // 回弹一下的效果
        anim.interpolator = android.view.animation.OvershootInterpolator()
        anim.addUpdateListener {
            animProgress = it.animatedValue as Float
            invalidate()
        }
        anim.start()
    }


}